<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_sendgoods_act',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_sendgoods/detail//id='.$item['id'],true);
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			$item['goods']=$goods;
			$item['endstatus']=$item['starttime']<time() ? $item['endtime']>time()? 1 : 2 : 0;
			$item['statustitle']=$item['starttime']<time() ? $item['endtime']>time()? '去' : '已结束' : '未开始';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'15'),
			
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_sendgoods_act',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']));
		$item['price']=$goods['price'];
		$item['original']=$goods['originalprice'];
		$item['total']=$goods['total'];
		$item['images']=json_decode($goods['images'],true);
	}else{
		$item=array();
	}
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('name'=>'goodsid','title'=>'选择商品','type'=>'choose_up','action'=>'get_goods'),
								array('type'=>'string','title'=>'商品名称','name'=>'title','help'=>'','must'=>true),
								array('name'=>'categoryid','title'=>'选择分类','type'=>'select','options'=>'category'),	
								array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
								array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
								array('type'=>'string','title'=>'虚拟已出售数量','name'=>'sales','help'=>''),
								array('type'=>'string','title'=>'排序','name'=>'displayorder','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>''),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),					
							)
						),
			
			)
	);
	$return['category']=pdo_getall('jy_sendgoods_category',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善标题','error');
	}
	if(!$postdata['categoryid']){
		exi('请选择分类','error');
	}
	if(empty($postdata['starttime'])){
		exi('请选择开始时间','error');
	}
	if(empty($postdata['endtime'])){
		exi('请选择结束时间','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'image'=>trim($postdata['images']['0']),
		'goodsid'=>intval($postdata['goodsid']),
		'categoryid'=>intval($postdata['categoryid']),
		'sales'=>intval($postdata['sales']),
		'displayorder'=>intval($postdata['displayorder']),
		'ishot'=>intval($postdata['ishot']),
		'status'=>intval($postdata['status']),
		'endtime'=>intval(strtotime($postdata['endtime'])),
		'starttime'=>intval(strtotime($postdata['starttime'])),
		'createtime'=>TIMESTAMP,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_sendgoods_act',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_sendgoods_act',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_sendgoods_act',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的商品！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_sendgoods_act',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_sendgoods_act',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
